package com.group.three.controller.orderController;

import com.group.three.entity.myOrder.MemberAddress;
import com.group.three.service.myorderservice.MemberAddressBuyerService;
import com.group.three.untils.SnowFlakeUtil;
import com.group.three.vo.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.websocket.server.PathParam;
import java.util.List;

/**
 * 买家端,会员地址接口
 */
@RestController
@Api(tags = "买家端,会员地址接口")
@RequestMapping("/buyer/member/address")
public class MemberAddressBuyerController {
    @Autowired
    private MemberAddressBuyerService memberAddressBuyerService;

    /**
     * 会员收件地址
     */
    @ApiOperation(value = "获取会员收件地址")
    @GetMapping
    public JsonResult AddressBuyer(String memberId) {
        try {
            List<MemberAddress> addressByMember = memberAddressBuyerService.getAddressByMember(memberId);
            return new JsonResult("200", "会员收件地址查询成功", addressByMember);
        } catch (Exception e) {
            return new JsonResult("400", "会员收件地址查询失败");
        }
    }

    /**
     * 会员收件地址详情
     */
    @ApiOperation(value = "获取会员收件地址详情")
    @GetMapping("/get/{id}")
    public JsonResult AddressItem(@PathVariable("id")Long id) {
        try {
            MemberAddress addressItem = memberAddressBuyerService.getAddressItem(id);
            return new JsonResult("200", "收件地址详情查询成功", addressItem);
        } catch (Exception e) {
            return new JsonResult("400", "收件地址详情查询失败");

        }

    }
    @ApiOperation(value = "修改会员收件地址")
    @PostMapping("/updateAddress")
    public  JsonResult editShippingAddress(@Valid MemberAddress shippingAddress) {
        int i=memberAddressBuyerService.updateMemberAddress(shippingAddress);
        if(i==1){
            return new JsonResult("200","修改地址成功");
        }
        return new JsonResult("400","修改地址失败");
    }

    @ApiOperation(value = "增加会员收件地址")
    @PostMapping("/addAddress")
    public  JsonResult addAddress(@Valid MemberAddress shippingAddress) {
        String s = String.valueOf(SnowFlakeUtil.getSnowFlakeId());
        String substring = s.substring(0, s.length() - 3);
        shippingAddress.setId(substring);
        int i=memberAddressBuyerService.addMemberAddress(shippingAddress);
        if(i==1){
            return new JsonResult("200","增加地址成功");
        }
        return new JsonResult("400","增加地址失败");
    }

    @ApiOperation(value = "删除会员收件地址")
    @ApiImplicitParam(name = "id", value = "会员地址ID", dataType = "String", paramType = "path")
    @DeleteMapping(value = "/delById/{id}")
    public JsonResult delShippingAddressById(@PathVariable String id) {
        int i=memberAddressBuyerService.removeMemberAddress(id);
        if(i==1){
            return new JsonResult("200","删除地址成功");
        }
        return new JsonResult("400","删除地址失败");
    }

}
